import axios from 'axios'
import router from '@/router';
import { getToken } from '@/utils/auth'
import { MessageBox, Message } from 'element-ui'
// import store from '@/store'

// create an axios instance
const service = axios.create({
    baseURL: process.env.VUE_APP_BASE_API, // url = base url + request url
    withCredentials: true,
    timeout: 15000
})

// request interceptor
service.interceptors.request.use(
    config => {
        let token = getToken() || '';
        config.headers["UserToken"] = token;
        return config
            // if (store.getters.token) {
            //     let token = getCache("UserToken");
            //     config.headers["UserToken"] = token;
            // }
            // return config
    },
    error => {
        // do something with request error
        console.log(error) // for debug
        return Promise.reject(error)
    }
)

// response interceptor
service.interceptors.response.use(
    /**
     * If you want to get http information such as headers or status
     * Please return  response => response
     */

    /**
     * Determine the request status by custom code
     * Here is just an example
     * You can also judge the status by HTTP Status Code
     */
    response => {
        let status = response.data.code;
        console.log(status);
        switch (status) {
            case 1001:
                MessageBox.confirm(response.data.msg, '提示', {
                        confirmButtonText: '重新登录',
                        cancelButtonText: '取消',
                        closeOnClickModal: false,
                        showCancelButton: false,
                        showClose: false,
                        type: 'warning'
                    })
                    .then(() => {
                        router.push('/login')
                    })
                return false
                    // router.push('/login')
                    // return
            case 1002:
                Message({
                    message: response.data.msg || 'Error',
                    type: 'error'
                })
                return
            default:
                return response
        }
        // if the custom code is not 20000, it is judged as an error.
        // if (!(res.code === 10000) && res.code !== 105) {
        //     Message({
        //         message: res.msg || 'Error',
        //         type: 'error'
        //     })

        //     // 50008: Illegal token; 50012: Other clients logged in; 50014: Token expired;
        //     return Promise.reject(new Error(res.msg || 'Error'))
        // } else if (res.code === 106 || res.code === 105 || res.code === 107) {
        //     // to re-login
        //     MessageBox.confirm('登录超时，请重新登录', '提示', {
        //             confirmButtonText: '重新登录',
        //             cancelButtonText: '取消',
        //             closeOnClickModal: false,
        //             showCancelButton: false,
        //             showClose: false,
        //             type: 'warning'
        //         })
        //         .then(() => {
        //             store.dispatch('user/resetToken').then(() => {
        //                 location.reload()
        //             })
        //         })
        //     return false
        // } else {
        //     return res
        // }
    },
    error => {
        console.log('err' + error) // for debug
            // Message({
            //   message: error.message,
            //   type: 'error',
            //   duration: 5 * 1000
            // })
        return Promise.reject(error)
    }
)

export default service